package com.weixing.mall.provider.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.base.exception.BusinessException;
import com.weixing.mall.provider.mapper.DepotExtraMapper;
import com.weixing.mall.provider.model.domain.DepotExtra;
import com.weixing.mall.provider.model.dto.DepotExtraDto;
import com.weixing.mall.provider.model.vo.DepotExtraVo;
import com.weixing.mall.provider.service.IDepotExtraService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 仓库模块-仓库扩展表（存储编号、库位、序号信息） 服务实现类
 * </p>
 *
 * @author Small
 * @since 2020-05-23
 */
@Service
public class DepotExtraServiceImpl extends ServiceImpl<DepotExtraMapper, DepotExtra> implements IDepotExtraService {
    @Autowired
    private DepotExtraMapper depotExtraMapper;
    @Override
    public void delByCondition(Long depotId, Long productId, String depotSn) {
      if(depotId !=null && productId !=null){
          LambdaQueryWrapper<DepotExtra> lqw = new LambdaQueryWrapper();
          lqw.eq(DepotExtra::getDepotId,depotId);
          lqw.eq(DepotExtra::getProductId,productId);
          if(ObjectUtil.isNotEmpty(depotSn)){
              lqw.eq(DepotExtra::getDepotSn,depotSn);
          }
          baseMapper.delete(lqw);
      }
    }

    @Override
    public IPage<DepotExtraVo> selectPage(Page page, Long depotId, Long productId){
        return depotExtraMapper.selectPage(page, depotId, productId);
    }

    @Override
    public void saveOrUpdate(DepotExtraDto ded){
        if(ObjectUtil.isEmpty(ded)){
            throw new BusinessException("对象信息为空，请刷新后重试！");
        }
        if(ded.getId() !=null){
            DepotExtra de = new DepotExtra();
            BeanUtils.copyProperties(ded,de);
            updateById(de);
        }else{
            DepotExtra de = new DepotExtra();
            BeanUtils.copyProperties(ded,de);
            save(de);
        }
    }

    @Override
    public void delById(Long id){
        removeById(id);
    }

    @Override
    public void delByDepotSn(String depotSn) {
        if(StrUtil.isNotEmpty(depotSn)){
            LambdaQueryWrapper<DepotExtra> lqw = new LambdaQueryWrapper();
            lqw.eq(DepotExtra::getDepotSn,depotSn);
            baseMapper.delete(lqw);
        }
    }
}
